package kevin.framework.basic.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import kevin.framework.basic.common.tree.ListToTree;
import kevin.framework.basic.common.ui.ClientButton;
import kevin.framework.basic.dao.IBaseDao;
import kevin.framework.basic.dao.IOrgDao;
import kevin.framework.basic.domain.Org;
import kevin.framework.basic.service.IOrgService;

/**
*Generate by Kevin's CodeBuilder
**/
@Service
public class OrgService extends BaseService<Org, String> implements IOrgService {

		private static final long serialVersionUID = -1L;

		IOrgDao orgDao;
		
		@Resource(name="orgDao")
		@Override
		public void setDao(IBaseDao<Org, String> dao) {
				this.dao = dao;
				orgDao = (IOrgDao)dao;
		}
		
		private static ListToTree<Org> toTree;

		public OrgService() {
			toTree = new ListToTree<Org>(this);
		}

		@Override
		public int deleteBatch(List<String> list) throws Exception {
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("idList", list);
			List<Org> listOrg = this.selectParam(param);
			for(Org m : listOrg) {
				Map<String,Object> params = new HashMap<String,Object>();
				params.put("id", m.getId());
				params.put("pid", m.getPid());
				orgDao.updatePid(params);
			}
			return super.deleteBatch(list);
		}
		
		@Override
		public JSONArray getOrgJsonByPid(String pid, List<ClientButton> buttonlist) throws Exception {
			Map<String,Object> params = new HashMap<String,Object>();
			return this.getOrgJsonByPid(pid, buttonlist, params);
		}
		//每次查询两级数据，剩下的ajax异步
		@Override
		public JSONArray getOrgJsonByPid(String pid, List<ClientButton> buttonlist, Map<String, Object> params) throws Exception {
			if(params == null) {
				params = new HashMap<String,Object>();
			}
			params.put("pid", pid);
			List<Org> list =  this.selectParam(params);
			List<String> pidList = new ArrayList<String>();
			for(Org r : list) {
				pidList.add(r.getId());
			}
			List<Org> childs = null;
			if(pidList.size() > 0 ) {
				params = new HashMap<String,Object>();
				params.put("pidList", pidList);
				childs = this.selectParam(params);
				list.addAll(childs);
			}		
			toTree.setBtnList(buttonlist);
			boolean needTools = true;
			if(buttonlist.size() == 0) {
				needTools = false;
			}
			JSONArray res = new JSONArray();
			try {
				res = toTree.getJSONArray(pid, list, needTools);
			} catch (Throwable e) {
				logger.error("",e);
			}
			return res;
		}

		@Override
		public JSONArray getOrgJsonByPid(String pid) throws Exception {			
			List<ClientButton> buttonlist = new ArrayList<ClientButton>();
			Map<String,Object> params = new HashMap<String,Object>();
			return this.getOrgJsonByPid(pid, buttonlist, params);
		}

		@Override
		public JSONArray getOrgJsonByPid(String pid, Map<String, Object> params) throws Exception {
			List<ClientButton> buttonlist = new ArrayList<ClientButton>();
			return this.getOrgJsonByPid(pid, buttonlist, params);
		}

		@Override
		public void createNode(Org bean, JSONObject obj, Object extParams) {
						
		}

		@Override
		public String getTreeId(Org bean, Object extParams) {
			// TODO Auto-generated method stub
			return bean.getId();
		}

		@Override
		public String getTreeText(Org bean, Object extParams) {
			// TODO Auto-generated method stub
			return bean.getOrgName();
		}

		@Override
		public String getTreePid(Org bean, Object extParams) {
			// TODO Auto-generated method stub
			return bean.getPid();
		}

		@Override
		public boolean isTreeLeaf(Org bean, Object extParams) {
			// TODO Auto-generated method stub
			return bean.getOrgType().equals(0);
		}

		@Override
		public ClientButton createTreeButton(ClientButton btn, Org bean, Object extParams) {
			if(!bean.getPid().equals(0) && "lineChildFn".equals(btn.getHandler())) {
				return null;
			}
			if(bean.getOrgType().equals(0) && "lineAddFn".equals(btn.getHandler())) {
				return null;
			}			
			return btn;
		}
		
}
